package com.kotlin.flong.base1

/**
 * 父类泛型对象可以赋值给子类泛型对象，用in关键字
 * 子类泛型对象可以赋值给父类泛型对象，用out关键字
 */

//out
interface Production<out T> {
    fun product(): T
}

//in
interface Consumer<in T> {
    fun consume(item: T)
}

//不变
interface ProductionConsumer<T> {
    fun product(): T
    fun consume(item: T)
}


//食物
open class Food

//快餐
open class FastFood : Food()

//汉堡包
open class Burger : FastFood()


/**
 * 生产者
 * 食品商店
 */
class FoodStore : Production<Food> {
    override fun product(): Food {
        println("product food")
        return Food()
    }
}

/**
 * 生产者
 * 快餐店
 */
class FastFoodStore : Production<FastFood> {
    override fun product(): FastFood {
        println("product fastFood")
        return FastFood()
    }
}

/**
 * 生产者
 * 汉堡商店
 */
class Burgertore : Production<Burger> {
    override fun product(): Burger {
        println("product Burger")
        return Burger()
    }
}

/**
 * 消费者
 *
 */
class EveryBody : Consumer<Food> {
    override fun consume(item: Food) {
        println("eat food ")
    }
}


/**
 * 消费者
 * 中国人
 */
class ChinaPeople : Consumer<FastFood> {
    override fun consume(item: FastFood) {
        println("eat FastFood ")
    }
}


/**
 * 消费者
 * 美国人
 */
class AmericanPeople : Consumer<Burger> {
    override fun consume(item: Burger) {
        println("eat Burger ")
    }
}


fun main() {
    var production1: Production<Food> = FoodStore()
    var production2: Production<Food> = FastFoodStore()
    var production3: Production<Food> = Burgertore()

    var consumer1: Consumer<Burger> = EveryBody()
    var consumer2: Consumer<Burger> = ChinaPeople()
    var consumer3: Consumer<Burger> = AmericanPeople()

}















